package actions;

import java.awt.Color;
import java.util.ArrayList;


import modele.Graphe;
import modele.Noeud;
import motor.MotorPerso;
import utils.Vector2d;


public class Deplacement extends Action
{
	private Graphe	graphe;
	ArrayList<Noeud> chemins = new ArrayList<Noeud>();

	private Vector2d	arrive;
	private Noeud	arriveNoeud;

	private int current;

	public Deplacement( Graphe g , Vector2d arrive)
	{
		this.graphe = g;
		this.arrive = arrive;
	}

	public void initialisation()
	{
		arriveNoeud = graphe.noeudNearest( arrive );

		chemins = graphe.trouverChemin( cible.bary , arrive );
		
		current = 0;
		
		for( int i = 0 ; i < chemins.size() ; i++ )
		{
			chemins.get(i).color = Color.DARK_GRAY;
			//System.out.println(chemins.get(i).temp);
		}
		System.out.println();
	}

	public void avancement(Vector2d vtemp, MotorPerso m)
	{
		
		attractCurrent( vtemp );
		
		float dis = chemins.get(current).distance( cible.getBary() );
		
		//System.out.println(dis);
		if( dis == 0 )
		{
			//System.out.println("CHANGEMENT");

			initialisation();
			chemins.remove(0);
			
			if( chemins.size() == 0 )
			{
				//System.out.println("FIN");
				cible.setAction(null);
			}
		}
	}

	private boolean attractCurrent(Vector2d vtemp)
	{
		float dis = chemins.get(current).distance( cible.getBary() );
		
		if( dis == 0 )
		{
			return true;
		}
		
		vtemp.x = -(cible.getBary().x - chemins.get(current).pos.x);
		vtemp.y = -(cible.getBary().y - chemins.get(current).pos.y);
		//System.out.println("num " + current );
		//System.out.println("current " + chemins.get(current) );
		//System.out.println("bary" + cible.getBary() );
		
		//System.out.println("Vtemp " + vtemp );
		
		//System.out.println();
		
		return false;
	}
}
